import { constantRoutes, asyncRouter } from "@/router";
const state = () => {
  return {
    routes: constantRoutes,
  };
};
const mutations = {
  setRoutes(state, otherRoutes) {
    state.routes = [...constantRoutes, ...otherRoutes];
  },
  removeRoutes(state) {
    state.routes = [];
  },
};

const actions = {
  // 筛选动态路由表
  filtersRoutes({ commit }, menus) {
    // 基于动态路由表和menus数组筛选
    const otherRoutes = asyncRouter.filter((item) =>
      menus.includes(item.children[0].name)
    );
    // 提交mutation
    commit("setRoutes", otherRoutes);
    // 返回筛选结果(给路由守卫那边用)
    return otherRoutes;
  },
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
